<template>
  <!-- 我的 -->
  <view class="mine-container" :style="{height: `${windowHeight}px`}">

    <view class="menu-list">
      <!-- 宫格组件 -->
      <uni-section title="功能列表"></uni-section>
      <view class="list-cell list-cell-arrow" v-for="menu in menuList" :key="menu.id" :index='menuList.length'>
        <view class="menu-item-box" @click="changeGrid(menu)">
          <view class="iconfont icon-setting menu-icon"></view>
          <view>{{menu.name}}</view>
        </view>
      </view>
    </view>

  </view>
</template>

<script>
import storage from '@/utils/storage'
import * as SupplierSendOrderApi from '@/api/suppliersendorder/inde.js' // 随货同行单

export default {
  onLoad() {
    this.getInfo()
    // 监听全局菜单更新事件
    this.$menuEventBus.$on('menusUpdated', this.handleMenusUpdated)
  },
  onUnload() {
    // 页面卸载时移除事件监听
    this.$menuEventBus.$off('menusUpdated', this.handleMenusUpdated)
  },
  data() {
    return {
      menuList: [], // 初始化为空数组
      name: this.$store.state.user.name,
      version: getApp().globalData.config.appInfo.version
    }
  },
  computed: {
    avatar() {
      return this.$store.state.user.avatar
    },
    windowHeight() {
      return uni.getSystemInfoSync().windowHeight - 50
    }
  },
  methods: {
    changeGrid(e) {
      uni.navigateTo({
        url: '/' + e.path
      })
    },
    async getInfo() {
      try {
        // 获取用户信息，这会触发全局事件
        await this.$store.dispatch('GetInfo')
        
        // 从Vuex状态中获取菜单数据
        const menus = this.$store.state.user.menus
        if (menus && menus.length > 0) {
          this.menuList = menus[0].children
        } else {
          this.menuList = []
        }
      } catch (error) {
        console.error('获取用户信息失败:', error)
        this.menuList = []
      }
    },
    // 处理全局菜单更新事件
    handleMenusUpdated(menus) {
      if (menus && menus.length > 0) {
        this.menuList = menus[0].children
      } else {
        this.menuList = []
      }
    },
    handleToInfo() {
      this.$tab.navigateTo('/pages/mine/info/index')
    },
    handleToSetting() {
      this.$tab.navigateTo('/pages/mine/setting/index')
    },
    handleToLogin() {
      this.$tab.reLaunch('/pages/login')
    },
    handleToAvatar() {
      this.$tab.navigateTo('/pages/mine/avatar/index')
    },
    handleLogout() {
      this.$modal.confirm('确定注销并退出系统吗？').then(() => {
        this.$store.dispatch('LogOut').then(() => {
          this.$tab.reLaunch('/pages/index')
        })
      })
    },
    handleJiaoLiuQun() {
      this.$modal.showToast('微信搜索 naidaguo 后，添加好友后拉你进技术交流群')
    },
    handleBuilding() {
      this.$modal.showToast('模块建设中~')
    },
    startScan() {
      uni.scanCode({
        success: (res) => {
          this.handleScanResult(res.result);
        },
        fail: (err) => {
          console.error('扫描失败:', err);
        }
      });
    },
    startScans() {
      uni.scanCode({
        success: (res) => {
          this.handleScanResult(res.result);
        },
        fail: (err) => {
          console.error('扫描失败:', err);
        }
      });
    },
    startScansss() {
      uni.reLaunch({
        url: `/pages/scanCodes/index?data=1`
      });
    },
    handleScanResult(scanData) {
      console.log('扫描结果', scanData);
      // 跳转暂时拿出来测试
      uni.navigateTo({
        url: `/pages/scanCode/index?data=${encodeURIComponent(scanData)}`
      });
      try {
        const id = scanData;
        SupplierSendOrderApi.getSupplierSendOrder(id).then(res => {
          console.log('API调用结果', res.data.statusTwo);

          // 判断扫描结果是不是随货同行单，为空不属于随货同行单
          if (res.data == null) {
            uni.showToast({
              title: '请扫描正确的随货同行单',
              icon: 'none',
              duration: 2000
            });
          } else if (res.data.statusTwo === '1') {
            uni.showToast({
              title: '该随货同行单已进行过入库操作，不可以重复操作',
              icon: 'none',
              duration: 5000
            });
          } else if (res.data.statusTwo === '0') {
            // 弹窗测试验证  需要删除
            uni.showToast({
              title: '正常操作随货同行单',
              icon: 'none',
              duration: 5000
            });
            uni.reLaunch({
              url: `/pages/scanCode/index?data=${encodeURIComponent(scanData)}`
            });
          }
        }).catch(error => {
          console.error(error)
        });
      } finally {
        // 这里可以添加一些清理代码，如果需要的话
      }
    },
    handleScanResultss(scanData) {
      console.log('扫描结果', scanData);
      // 跳转暂时拿出来测试
      uni.navigateTo({
        url: `/pages/scanCodes/index?data=${encodeURIComponent(scanData)}`
      });
      try {
        const id = scanData;
        SupplierSendOrderApi.getSupplierSendOrder(id).then(res => {
          console.log('API调用结果', res.data.statusTwo);

          // 判断扫描结果是不是随货同行单，为空不属于随货同行单
          if (res.data == null) {
            uni.showToast({
              title: '请扫描正确的随货同行单',
              icon: 'none',
              duration: 2000
            });
          } else if (res.data.statusTwo === '1') {
            uni.showToast({
              title: '该随货同行单已进行过入库操作，不可以重复操作',
              icon: 'none',
              duration: 5000
            });
          } else if (res.data.statusTwo === '0') {
            // 弹窗测试验证  需要删除
            uni.showToast({
              title: '正常操作随货同行单',
              icon: 'none',
              duration: 5000
            });
            uni.reLaunch({
              url: `/pages/scanCode/index?data=${encodeURIComponent(scanData)}`
            });
          }
        }).catch(error => {
          console.error(error)
        });
      } finally {
        // 这里可以添加一些清理代码，如果需要的话
      }
    }
  }
}
</script>

<style lang="scss">
page {
  background-color: #f5f6f7;
}
.uni-grid-item {
  width: 100%; /* 设置你想要的宽度 */
}
.mine-container {
  width: 100%;
  height: 100%;

  .header-section {
    padding: 15px 15px 45px 15px;
    background-color: #3c96f3;
    color: white;

    .login-tip {
      font-size: 18px;
      margin-left: 10px;
    }

    .cu-avatar {
      border: 2px solid #eaeaea;

      .icon {
        font-size: 40px;
      }
    }

    .user-info {
      margin-left: 15px;

      .u_title {
        font-size: 18px;
        line-height: 30px;
      }
    }
  }

  .content-section {
    position: relative;
    top: -50px;

    .mine-actions {
      margin: 15px 15px;
      padding: 20px 0px;
      border-radius: 8px;
      background-color: white;

      .action-item {
        .icon {
          font-size: 28px;
        }

        .text {
          display: block;
          font-size: 13px;
          margin: 8px 0px;
        }
      }
    }
  }
}
</style>
